package com.dz.product.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.dz.common.entity.PageVO;
import com.dz.product.entity.Product;
import com.dz.product.entity.ProductQueryVO;
import com.dz.product.service.ProductService;

/**
 * <p>
 * 商品基础表 前端控制器
 * </p>
 *
 * @author star
 * @since 2017-06-22
 */
@RestController
@Api("产品API")
public class ProductControler {

	@Autowired
	private ProductService productService;

	@RequestMapping(value = "/list")
	@ApiOperation(value = "获取产品列表", httpMethod = "POST")
	public PageVO<Product> queryList(@ModelAttribute ProductQueryVO proQueryVO) {
		if (proQueryVO == null) {
			proQueryVO = new ProductQueryVO();
		}
		if (StringUtils.isBlank(proQueryVO.getLableIds())) {
			proQueryVO.setLableIds(null);
		}
		Pagination productPage = new Pagination(proQueryVO.getPage(), proQueryVO.getPageSize());
		List<Product> products = productService.queryProducts(productPage, proQueryVO);
		PageVO<Product> pageVO = new PageVO<Product>();
		pageVO.setTotal(productPage.getTotal());
		pageVO.setRecords(products);
		return pageVO;
	}

	@ApiOperation(value = "获取产品明细", httpMethod = "GET")
	@RequestMapping(value = "getById", method = RequestMethod.GET)
	public Product getById(@RequestParam(name = "id") Long id) {
		return productService.selectById(id);
	}
}
